package com.qiuzhiyi.fragment;

import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.qiuzhiyi.R;
import com.qiuzhiyi.activity.ResetPwdActivity;
import com.qiuzhiyi.base.QZYBaseFragment;
import com.qiuzhiyi.cache.HumanCache;
import com.qiuzhiyi.item.BaseItem;
import com.qiuzhiyi.item.HumanItem;
import com.qiuzhiyi.net.ObservableHelper;
import com.qiuzhiyi.net.RetrofitHelper;
import com.qiuzhiyi.net.SelectorObserver;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.disposables.Disposable;

/**
 * Author: Li
 * Date: 2017/11/14 0014
 * Description:
 */

public class PwdLoginFragment extends QZYBaseFragment {

    @BindView(R.id.et_phone)
    EditText mEtPhone;
    @BindView(R.id.et_pwd)
    EditText mEtPwd;
    @BindView(R.id.txt_login)
    TextView mTxtLogin;
    @BindView(R.id.txt_forget_pwd)
    TextView mTxtForgetPwd;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_login_pwd;

    }


    @OnClick({R.id.txt_login, R.id.txt_forget_pwd})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.txt_login:

                if (isEmpty(mEtPhone) || isEmpty(mEtPwd)) {
                    showToast("请输入账号密码");
                    return;
                }
                login(mEtPhone.getText().toString(), mEtPwd.getText().toString());
                break;
            case R.id.txt_forget_pwd:
                startThenFinish(ResetPwdActivity.class);
                break;
        }
    }

    /**
     * 登录
     *
     * @param account  账号
     * @param password 密码
     */
    private void login(String account, String password) {
        Map<String, String> map = new HashMap<>();
        map.put("humanAccount", account);
        map.put("humanPassword", password);
        map.put("loginStatus","2");
        ObservableHelper.createObservable(RetrofitHelper.createService().loginWithPwd(map))
                .subscribe(new SelectorObserver<BaseItem<HumanItem>>() {
                    @Override
                    protected void onErrorCodeInfo(String code, String info) {
                        showToast(info);
                    }

                    @Override
                    protected void onSuccess(BaseItem< HumanItem > humanItemBaseItem) {
                        HumanItem item = humanItemBaseItem.getObjValue();
                        HumanCache.saveHumanInfo(mContext, item);
                        showToast("登陆成功");
                        getActivity().finish();
//                        startThenFinish(MainActivity.class);
                    }

                    @Override
                    protected void onFinish() {
                        dismissLoadingDialog();
                    }

                    @Override
                    public void onSubscribe(Disposable d) {
                        addDisposable(d);
                        showLoadingDialog("正在登录");
                    }
                });

    }

}
